package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class MenuBar extends JToolBar{

	private static final long serialVersionUID = 1L;

	private BgButton[] actions;
	private JToggleButton autoPause;
	private JTextField timePause;
	
	/**
	 * the constructor of the menu where there are buttons to perform actions and communicate it to the Hypervisor
	 * @param al the Hypervisor
	 */
	public MenuBar(ActionListener al){
		setLayout(new GridBagLayout());
		setFloatable(false);
		setBackground(Color.BLACK);
		String[] buttons={"play","pause","stop"};
		actions = new BgButton[buttons.length];
		for (int i=0; i<buttons.length; i++){
			actions[i]= new BgButton(buttons[i] + ".png");
			actions[i].addActionListener(al);
			actions[i].setActionCommand(buttons[i]);
			add(actions[i], new GridBagConstraints(i,0,1,1,0,0,GridBagConstraints.LINE_START,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
		}
		actions[1].setEnabled(false);
		actions[2].setEnabled(false);
		autoPause = new JToggleButton("auto pause");
		autoPause.addActionListener(al);
		autoPause.setActionCommand("autopause");
		autoPause.setBackground(new Color(80,0,0));
		autoPause.setForeground(Color.WHITE);
		add(autoPause,new GridBagConstraints(buttons.length+1,0,1,1,0,0,GridBagConstraints.LINE_START,GridBagConstraints.NONE,new Insets(2,2,2,2),0,0));
		JLabel l = new JLabel("ms of pause:");
		l.setForeground(Color.WHITE);
		add(l,new GridBagConstraints(buttons.length+2,0,1,1,0,0,GridBagConstraints.LINE_START,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
		timePause = new JTextField("",3);
		timePause.setDocument(new IntegerDocument());
		timePause.setText("0");
		timePause.setHorizontalAlignment(CENTER);
		timePause.setFont(new Font("Verdana",Font.BOLD,15));
		add(timePause,new GridBagConstraints(buttons.length+3,0,1,1,0,0,GridBagConstraints.LINE_START,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
		add(Box.createGlue(),new GridBagConstraints(buttons.length+4,0,1,1,1.0,0,GridBagConstraints.LINE_START,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
	}
	
	/**
	 * this method return a JButton between play, pause and stop
	 * @param i the index of the requested button
	 * @return the requested button
	 */
	public JButton getButton(int i){
		return actions[i];
	}
	
	/**
	 * 
	 * @return the autopause ToggleButton
	 */
	public JToggleButton getAutoPause(){
		return autoPause;
	}
	
	/**
	 * 
	 * @return the autopause ToggleButton
	 */
	public void setEnableTimePause(boolean enabled){
		timePause.setEnabled(enabled);
	}
	
	/**
	 * 
	 * @return the ms of pause to be applied
	 */
	public int getPause(){
		return Integer.parseInt(timePause.getText());
	}
}
